﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace T03ChatServer
{
    class BroadCast
    {
        public static int SERVER_MSG = 1;
        public static int CLIENT_MSG = 2;
        public static void BroadMessage(string msg, string uname, Dictionary<string, Socket> clientList, int flag)
        {
            foreach (var item in clientList)
            {
                Socket _socket = item.Value;
                string datamsg = null;
                byte[] bytemsg = new byte[4096];
                if (flag == 1)
                {
                    datamsg = "【系统消息】" + msg + "\t\t" + DateTime.Now; 
                }
                else if (flag == 2)
                {
                    datamsg = uname +"："+ msg + "\t\t" + DateTime.Now;
                }
                bytemsg = Encoding.UTF8.GetBytes(datamsg);
                try
                {
                    _socket.Send(bytemsg);
                }
                catch (Exception e)
                {
                    _socket.Close();
                    _socket = null;
                    Debug.WriteLine(e.ToString());
                    continue;
                }
            }
        }
    }
}
